<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input :placeholder="$t('table.username')" v-model="listQuery.username" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"/>
      <el-input :placeholder="$t('table.name')" v-model="listQuery.name" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"/>
      <el-date-picker v-model="listQuery.creation_datetime" :placeholder="$t('table.creation_datetime')" type="datetime"/>
      <el-date-picker v-model="listQuery.modified_datetime" :placeholder="$t('table.modified_datetime')" type="datetime"/>
      <el-select v-model="listQuery.sort" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option v-for="item in sortOptions" :key="item.key" :label="item.label" :value="item.key"/>
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">{{ $t('table.add') }}</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      border
      stripe
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange">
      <el-table-column :label="$t('table.id')" sortable="custom" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.username')" align="center" width="65">
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.name')" align="center" width="65">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.creation_datetime')" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.creation_datetime }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.modified_datetime')" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.modified_datetime }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />


    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="width: 600px; margin-left:50px;">
        <el-form-item :label="$t('table.username')" prop="username">
          <el-input v-model="temp.username" style="width: 200px;" class="filter-item"/>
        </el-form-item>
        <el-form-item :label="$t('table.name')" prop="name">
          <el-input v-model="temp.name" style="width: 200px;" class="filter-item"/>
        </el-form-item>
        <el-form-item :label="$t('table.password')" prop="password">
          <el-input :type="password_type" v-model="temp.password" style="width: 200px;">
            <i slot="suffix" title="显示密码" style="cursor:pointer;" class="el-icon-view" @click="showPass"/>
          </el-input>
        </el-form-item>
        <el-form-item :label="$t('table.routes')">
          <el-transfer v-model="transform_data" :data="transform_ori_data"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleUpdate(scope.row)">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { fetchList } from '@/api/user'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      password_type: 'password',
      dialogStatus: undefined,
      transform_data: undefined,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        username: undefined,
        name: undefined,
        limit: 20,
        creation_datetime: undefined,
        modified_datetime: undefined,
        sort: '+id'
      },
      sortOptions: [{ label: this.$t('table.assending'), key: '+id' }, { label: this.$t('table.descending'), key: '-id' }],
      temp: {
        id: undefined,
        username: '',
        user: '',
        password: '',
        routes: '',
        creation_datetime: '',
        modified_datetime: ''
      },
      dialogFormVisible: false,
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        username: [{ required: true, message: this.$t('validate.username'), trigger: 'blur' }],
        name: [{ required: true, message: this.$t('validate.name'), trigger: 'blur' }],
        password: [{ required: true, message: this.$t('validate.password'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    transform_ori_data() {
      const data = []
      for (let i = 0; i < this.$store.state.permission.route_titles.length; i++) {
        data.push({
          key: this.$store.state.permission.route_titles[i],
          label: this.$store.state.permission.route_titles[i],
          disabled: false
        })
      }
      return data
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleUpdate(row) {
      this.temp.username = row.username
      this.temp.name = row.name
      this.temp.password = row.password
      this.temp.creation_datetime = row.creation_datetime
      this.temp.modified_datetime = row.modified_datetime
      this.temp.routes = row.routes
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    showPass() {
      this.password_type = 'text'
    }
  }
}
</script>
<style  rel="stylesheet/scss" lang="scss" scoped>
.app-container{
 .el-transfer-panel__body{
    height: 150px !important;
    background: red;
  }
}
</style>
